﻿using UnityEngine;
using System.Collections;

public class StartServer : MonoBehaviour {

    private ConnectionManager networkManager;

    public int numberOfPlayers = 4;

    public GameObject gameNetworkManager;   // Set in the editor; should be the "Game Network Manager" prefab

	// Use this for initialization
	void Start () {
        networkManager = GameObject.FindGameObjectWithTag("NetworkController").GetComponent<ConnectionManager>();
        networkManager.RunServer(numberOfPlayers);
	}
	
	// Update is called once per frame
	void Update () {

        if (Input.GetKeyDown(KeyCode.Escape))
        {
            Application.Quit();
        }

        if (networkManager.clients.Count == numberOfPlayers)
        {
            // TODO: Net-instantiate ServerGameEvents

            Network.Instantiate(gameNetworkManager, Vector3.zero, Quaternion.identity, 0);
            Application.LoadLevel("gameServer");
        }

	}

    void OnGUI()
    {
        GUI.color = Color.white;
        GUILayout.Label("Waiting for all clients to connect... " + Network.player.ipAddress);
    }
}
